let fetchCount = 0
let articleList = []
function fetchArticle() {
  fetch('https://api.juejin.cn/content_api/v1/article/query_list?aid=2608&uuid=7175507527969949195&spider=0', {
    headers: {
      accept: '*/*',
      'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6,zh-TW;q=0.5,fr;q=0.4,ko;q=0.3',
      'cache-control': 'no-cache',
      'content-type': 'application/json',
      pragma: 'no-cache',
      'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
      'sec-ch-ua-mobile': '?0',
      'sec-ch-ua-platform': '"Windows"',
      'sec-fetch-dest': 'empty',
      'sec-fetch-mode': 'cors',
      'sec-fetch-site': 'same-site',
      'x-secsdk-csrf-token':
        '0001000000019ef16fbe690d751de998498f3f6efd1050a7471e85fd26a04618adf2f8115d8c17738dc5933c0b74',
    },
    referrer: 'https://juejin.cn/',
    referrerPolicy: 'strict-origin-when-cross-origin',
    body: '{"user_id":"2893570333750744","sort_type":2,"cursor":"' + fetchCount * 10 + '"}',
    method: 'POST',
    mode: 'cors',
    credentials: 'include',
  })
    .then(res => res.json())
    .then(res => {
      if (res['err_no'] === 0) {
        articleList = articleList.concat(res.data)
        if (res['has_more']) {
          fetchCount++

          setTimeout(() => {
            fetchArticle()
          }, 1000 * 5)
        } else {
          parseData()
        }
      }
    })
    .catch(err => console.error(err))
}

function parseData() {
  const articleContainer = document.body
  articleList.forEach(a => {
    const { article_id, title } = a.article_info
    const articleLink = document.createElement('a')
    articleLink.href = 'https://juejin.cn/post/' + article_id
    articleLink.innerText = title
    articleContainer.appendChild(articleLink)
  })
}

fetchArticle()
